/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */
/**
 * Created by ued on 2016/11/4.
 */

import Vue from 'vue';
import Vuex from 'vuex';
import createLogger from 'vuex/dist/logger';
import * as TYPES from './mutation_types';
import * as actions from './actions';
import admin from './modules/admin';
import branch from './modules/branch';
import clusterDeploy from './modules/cluster_deploy';
import authorization from './modules/authorization';
import getBodyHeight from './get_body_height';
import getters from './getters';
import project from './modules/project';
import task from './modules/task';



Vue.use(Vuex);
const DEBUG = process.env.NODE_ENV !== 'production';
const INNER_HEIGHT = 800;
const WARN_HEADER_HEIGHT = 30;
const BIG_SCREEN_WIDTH = 1440;
const MIN_WIDTH = 1280;
const BIG_LEFT_NAV = 240;
const SMALL_LEFT_NAV = 200;
const BIG_SCREEN_NAV_WIDTH = 1608;

function checkIsFullScreen () {
    let explorer = window.navigator.userAgent.toLowerCase();

    if (explorer.indexOf('chrome') > 0) { // webkit
        return document.body.scrollHeight === window.screen.height && document.body.scrollWidth === window.screen.width;
    }

    // IE 9+  fireFox
    return window.outerHeight === window.screen.height && window.outerWidth === window.screen.width;

}

export default new Vuex.Store({

    strict: DEBUG,

    plugins: DEBUG ? [createLogger()] : [],

    state: {
        bodyHeight: 550,
        bodyWidth: 1280,
        leftNavWidth: 240,
        isBigScreen: false,
        contentHeight: 800,
        isFullScreen: false,
        isSecureVersion: false, // 当前是否为安全版本
        csrfToken: '', // 防CSRF的令牌
        rsaPublicKey: '', // 加密密码用的公钥
        rsaModulus: '', // 加密密码用的公钥的modulus
        account: '', // 用户账号
        userName: '', // 用户名
        roleType: {}, // 用户角色
        isEnglish: false, // 当前是否英文版本
        version: 'beta', // 当前版本号
        manageLink: '', // 运维管理url
        isAdmin:false, // 是否为管理员
        userId:'' // userId
    },

    getters,

    mutations: {
        updateHeight (state, isShowWarn) {
            let bodyHeight = getBodyHeight();

            state.bodyHeight = isShowWarn ? bodyHeight - WARN_HEADER_HEIGHT : bodyHeight;
            state.contentHeight = isShowWarn ? getBodyHeight(INNER_HEIGHT) - WARN_HEADER_HEIGHT : getBodyHeight(INNER_HEIGHT);
            state.isFullScreen = checkIsFullScreen();
        },
        updateWidth (state) {
            state.bodyWidth = Math.max(window.innerWidth, MIN_WIDTH);
            state.isBigScreen = window.outerWidth > BIG_SCREEN_WIDTH;
        },
        updateLeftNavWidth (state) {
            state.leftNavWidth = window.outerWidth >= BIG_SCREEN_NAV_WIDTH ? BIG_LEFT_NAV : SMALL_LEFT_NAV;
        },
        [TYPES.SET_IS_SECURE_VERSION] (state, isSecureVersion) {
            state.isSecureVersion = isSecureVersion;
        },
        [TYPES.SET_CSRF_TOKEN] (state, csrfToken) {
            state.csrfToken = csrfToken;
        },
        [TYPES.SET_RSA_PUBLIC_KEY] (state, rsaPublicKey) {
            state.rsaPublicKey = rsaPublicKey;
        },
        [TYPES.SET_RSA_MODULUS] (state, rsaModulus) {
            state.rsaModulus = rsaModulus;
        },
        [TYPES.SET_ACCOUNT] (state, account) {
            state.account = account;
        },
        [TYPES.SET_USERNAME] (state, userName) {
            state.userName = userName;
        },

        [TYPES.SET_USER_ROLY_TYPE] (state, roleType) {
            state.roleType = roleType;
        },

        [TYPES.SET_IS_ENGLISH] (state, isEnglish) {
            state.isEnglish = isEnglish;
        },
        [TYPES.SET_VERSION] (state, version) {
            state.version = version;
        },
        [TYPES.MANAGE_LINK] (state, manageLink) {
            state.manageLink = manageLink;
        },
        [TYPES.USERS_ID] (state, userId) {
            state.userId = userId;
        },
        [TYPES.IS_ADMIN] (state, isAdmin) {
            state.isAdmin = isAdmin;
        }
    },

    modules: {
        branch,
        admin,
        clusterDeploy,
        authorization,
        project,
        task
    },

    actions
});
